package com.cynthia.system.controller;


import com.cynthia.system.model.*;
import com.cynthia.system.service.ISysPermissionService;
import com.cynthia.utils.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author cynthia
 * @since 2021-12-15
 */
@Controller
@RequestMapping("/sys-permission")
public class SysPermissionController extends BaseController {

    /*@Autowired
    private ISysPermissionService iSysPermissionService;*/

    /*-----查询-----权限列表-----*/
    @RequestMapping("/list")
    public String list(Model model){
        //内置的方法list
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);
        System.out.println("===查询权限列表===list()===");
        return "/view/system/permission/permissionList";
    }

    /*-----添加UI-----权限-----*/
    @RequestMapping("/addUI")
    public String addUI(Model model){
        //查询数据库获取父节点
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);
        System.out.println("===添加UI===addUI()===");
        return "/view/system/permission/addPermission";
    }

    /*-----添加-----权限-----*/
    @RequestMapping("/add")
    public String add(Model model,SysPermission sysPermission){
        //获取数据
        System.out.println(sysPermission);
        //调用业务
        boolean b=iSysPermissionService.save(sysPermission);
        //返回PermissionList页面
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);
        System.out.println("===添加权限===add()===");
        return "/view/system/permission/permissionList";
    }

    /*-----删除-----通过PermissionId删除权限-----*/
    @RequestMapping("/delete")
    public String delete(Model model,int permissionId){
        //获取数据
        System.out.println(permissionId);
        //调用业务
        boolean b=iSysPermissionService.removeById(permissionId);
        //返回PermissionList页面
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);
        System.out.println("===删除权限===delete()===");
        return "/view/system/permission/permissionList";
    }

    /*-----修改UI-----权限-----*/
    @RequestMapping("/updateUI")
    public String updateUI(Model model,int permissionId){
        //接收数据
        System.out.println(permissionId);
        //调用业务
        //通过 permissionId 权限ID查询对应的权限信息
        SysPermission sysPermission_db=iSysPermissionService.getById(permissionId);
        model.addAttribute("sysPermission_db",sysPermission_db);
        //查询数据库获取父节点
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);
        System.out.println("===修改UI===updateUI()===");
        //返回updatePermission页面
        return "/view/system/permission/updatePermission";
    }

    /*-----修改-----权限-----*/
    @RequestMapping("/update")
    public String update(Model model,SysPermission sysPermission){
        //获取数据
        System.out.println(sysPermission);
        //调用业务进行更新
        boolean b=iSysPermissionService.saveOrUpdate(sysPermission);
        //返回PermissionList页面
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);
        System.out.println("===修改权限===update()===");
        return "/view/system/permission/permissionList";
    }

    /*-----分配权限UI-----*/
    @RequestMapping("/FPPermissionUI")
    public String FPPermissionUI(Model model,int roleId){

        //获取对应的角色信息
        SysRole sysRole_db=iSysRoleService.getById(roleId);
        model.addAttribute("sysRole_db",sysRole_db);
        System.out.println("数据库返回的角色信息："+sysRole_db);
        //准备父节点(权限列表)
        List<SysPermission> sysPermissionList_db=iSysPermissionService.queryPermissionList();
        model.addAttribute("sysPermissionList_db",sysPermissionList_db);

        System.out.println("===分配权限UI===FPPermissionUI()===");
        //返回FPPermission页面
        return "/view/system/permission/FPPermission";
    }


    /*-----分配权限-----*/
    @RequestMapping("/FPPermission")
    public String FPPermission(Model model, int roleId, int[] permissionId,SysRolePermission sysRolePermission){

        //获取角色id
        System.out.println("获取角色id:"+roleId);
        //获取权限id
        System.out.println("获取权限id:"+sysRolePermission.getPermissionId());

        //当同一个roleId多次分配权限时，数据库会多次记录，为避免重复
        //先判断数据库中是否已经有了记录
        boolean flag=iSysRolePermissionService.deleteRolePermissionByRoleId(roleId);

        for (int permissionID:permissionId){
            sysRolePermission.setRoleId(roleId);
            sysRolePermission.setPermissionId(permissionID);
            //保存数据
            iSysRolePermissionService.save(sysRolePermission);
        }
        //返回role-list页面
        List<SysRole> sysRoleList_db = iSysRoleService.list();
        model.addAttribute("sysRoleList_db",sysRoleList_db);
        return "view/system/role/roleList";

       /* //返回FPPermission页面
        return "/view/system/permission/FPPermission";*/
    }
}